package com.library.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.library.common.entity.MyPage;
import com.library.common.utils.PageUtil;
import com.library.entity.ResourceAudit;
import com.library.entity.ResourceEntity;
import com.library.entity.UserInfo;
import com.library.entity.UserResource;
import com.library.entity.dto.FeedBackDto;
import com.library.entity.dto.UserResourceDto;
import com.library.mapper.UserResourceMapper;
import com.library.service.ResourceService;
import com.library.service.UserInfoService;
import com.library.service.UserResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.library.util.MoJiCommonUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户浏览资源关联表 服务实现类
 * </p>
 *
 * @author ccl
 * @since 2025-02-19
 */
@Service
public class UserResourceServiceImpl extends ServiceImpl<UserResourceMapper, UserResource> implements UserResourceService {

    @Resource
    private ResourceService resourceService;
    @Resource
    private UserInfoService userInfoService;


    @Override
    public MyPage<ResourceEntity> queryPage(UserResourceDto dto) {
        LambdaQueryWrapper<UserResource> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserResource::getOpenId, dto.getOpenId());
        wrapper.orderByDesc(UserResource::getCreatedTime);
        List<UserResource> userResourceList=this.list(wrapper);
        if(!userResourceList.isEmpty()){
            List<Long> idList=userResourceList.stream().map(UserResource::getResourceId).collect(Collectors.toList());
            LambdaQueryWrapper<ResourceEntity> wrapper1 = Wrappers.lambdaQuery();
            wrapper1.in(ResourceEntity::getResourceId,idList);
            wrapper1.eq(ResourceEntity::getDataTypeId,dto.getDataTypeId());
            PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
            List<ResourceEntity> resourceEntities=resourceService.list(wrapper1);
            PageInfo<ResourceEntity> info = new PageInfo<>(resourceEntities);
            return PageUtil.PageInfo2MyPage(info);
        }
        List<ResourceEntity> resourceEntityList=new ArrayList<>();
        PageInfo<ResourceEntity> info = new PageInfo<>(resourceEntityList);
        return PageUtil.PageInfo2MyPage(info);
    }

    @Override
    public ResourceEntity detail(Long id) {
        return resourceService.getById(id);
    }

    @Override
    public boolean add(UserResource dto) {
        LambdaQueryWrapper<UserResource> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserResource::getOpenId, dto.getOpenId());
        wrapper.eq(UserResource::getResourceId, dto.getResourceId());
        List<UserResource> userResourceList=this.list(wrapper);
        if(userResourceList.isEmpty()){
            dto.setCreatedTime(new Date());
            return this.save(dto);
        }
        return true;
    }
}
